<template>
	<view class="login_box">
		<view class="img_box">
			<img class="img_box-image" src="https://image.nanjingtouyang.com/touy/2024/05/11/9970b19ef2194c7c8429eea9182f8576.png"/>
			<view class="login_tit">
				<h1>透氧教练端</h1>
			</view>
			
			<button v-if="agrBtn==true" class="login-btn" type="primary" @click="getUserInfo" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" style="background-color:#46973B;">
			  <h1 style="color: #fff;">手机号快捷登录</h1>
			</button>
			
			<button v-else class="login-btn" type="primary" @click="toChecked" style="background-color:#46973B;">
			  <h1 style="color: #fff;">手机号快捷登录</h1>
			</button>
			
			<view class="agree-box-view">
				<view class="agreement_btn">
					<checkbox-group @change="checkboxChange">
					<label>
						<checkbox value="cb" color="#46973B;" style="transform:scale(0.7)" :checked="agrBtn" />
					</label>
					</checkbox-group>
				</view>
				<view class="agreement_tit">
					<h1>我已阅读并同意<span class="agreement_xieyi" @click="userAgreement">透氧教练协议、</span><span class="agreement_xieyi" @click="privacyAgreement">隐私协议</span></h1>
				</view>
				<view class="mask_box" v-show="mask">
					<h1>请先阅读用户协议</h1>
				</view>
			</view>
		</view>
	</view>
	
</template>

<script>
	import { userLogin, getUserPhone } from "@/api/public.js"
	import { editUserInfo } from "@/api/mine.js"
	export default {
		data(){
			return{
				wxcode:'',
				token:'',
				phonecode:'',
				phone:'',
				nickname:'',
				avatar:'',
				wxOpenId:'',
				userId:'',
				statusCode:'',
				agrBtn:false,
				mask:false,
				isChecked:0,
				
			}
		},
		// onUnload(){
		// 	if(uni.getStorageSync('fromActivity') == 1) {
			
		// 	}else {
		// 		uni.switchTab({
		// 			url:'/pages/index/index'
		// 		})
		// 	}
			
		// },
		created() {
		},
		onLoad(option){
			uni.setStorageSync('fromActivity', option.fromActivity)
			
			
		},
		
		methods:{
			onBack() {
				// this.$emit("onBack")
				// uni.navigateBack()
				// uni.switchTab({
				// 	url:'/pages/index/index'
				// })
			},
			//登录
			getUserInfo() { 
				const _that=this;
				if(_that.isChecked==0){
					uni.showToast({
						title:'请勾选',
						duration:2000,
						icon:'error'
					})
					return;
				}
			     // 展示加载框
			     uni.showLoading({
			       title: '加载中',
			     });
				 uni.login({
				   provider: 'weixin',
				   success: (res) => {
					console.log('微信code码',res);
					_that.wxcode = res.code; //code码	
				   },
				 });
			   
			   }, 
			   async getPhoneNumber(e){
					const _that=this;
					console.log('phonecode----------',e);
			   		_that.phonecode=e.detail.code //code码	
					_that.encryptedData = e.detail.encryptedData
					let submitData
					if(uni.getStorageSync('invite_id')) {
						submitData = {
							openIdCode: _that.wxcode,
							encryptedData: _that.encryptedData,
							iv: uni.getStorageSync('invite_id'),
						}
					} else {
						submitData = {
							openIdCode: _that.wxcode,
							encryptedData: _that.encryptedData,
						}
					}
					
			   		if(e.detail.code){
						uni.showToast({
							icon:'loading',
							title:'登录中'
						})
						setTimeout(()=>{
							// 登录获取token
							userLogin(submitData)
							.then(res => {
								console.log(res.data)
								uni.setStorageSync('spToken', res.data.data.access_token)
								uni.setStorageSync('spUid', res.data.data.user_id)
								uni.setStorageSync('firstLogin', true)
								// getUserDetail(uni.getStorageSync("uid")).then(res => {
								// 	uni.setStorageSync('wxname', res.data.data.userName)
								// })
								
								
								getUserPhone({
									openIdCode: _that.phonecode
								}).then(res => {
									uni.setStorageSync('spPhone', res.data.data)
									// uni.navigateBack()
									editUserInfo({
										userId: uni.getStorageSync('spUid'),
										phone: res.data.data
									}).then(res => {
										
											uni.navigateBack()
										
										
										
										
									})
									
								})
								
							})

								
						},2000)	
					}
			   		  
			   	
			   	},
				
				toChecked() {
					uni.showToast({
						title:'请先阅读用户协议',
						duration:2000,
						icon:'none'
					})
				},
			checkboxChange(e){
				
				this.isChecked=e.detail.value.length
				this.agrBtn = !this.agrBtn
			},
			userAgreement(){
				uni.navigateTo({
					url:"../../pages/user-agreement/index"
				})
			},
			privacyAgreement(){
				uni.navigateTo({
					url:"../../pages/privacy-agreement/index"
				})
			}
		
	   
		},
		
		}
</script>

<style lang="scss">
	@import './login.scss';
</style>